import { HashMap } from "../../api/struct/HashMap";
import { E_BattleReportType } from "../defs/battle/E_BattleReportType";
import { CDStage } from "../excel/CDStage";


/**
 * 扩展-主线
 */
export abstract class CEStage {

    /**后置关卡 */
    public static nextStage = new HashMap<number, number>();
    /**章节关卡 */
    public static chapterStage = new HashMap<number, HashMap<number, number[]>>();
    /**试练塔关卡 */
    public static trialTowerStage = new HashMap<number, number>();
    /**世界boss关卡(关卡id, 1/0) */
    public static worldBossStage = new HashMap<number, number>();
    /**世界boss2关卡(关卡id, 1/0) */
    public static worldBossStage2 = new HashMap<number, number>();
    /**世界boss3关卡(关卡id, 1/0) */
    public static worldBossStage3 = new HashMap<number, number>();
    /**军团boss关卡(关卡id, 1/0) */
    public static legionBossStage = new HashMap<number, number>();

    /**初始化 */
    public static init(): void {
        const vs = CDStage.datas.getValues();

        for (let i = 0, len = vs.length; i < len; i++) {
            const config = vs[i];
            if (config.preStage > 0) {
                this.nextStage.put(config.preStage, config.id);
            }

            if (config.chapter != 0
                && config.type === E_BattleReportType.MainStage) {
                let chapterMap = this.chapterStage.get(config.chapter);
                if (!chapterMap) {
                    chapterMap = new HashMap<number, number[]>();
                    this.chapterStage.put(config.chapter, chapterMap);
                }
                let pageList = chapterMap.get(config.page);
                if (!pageList) {
                    pageList = [];
                    chapterMap.put(config.page, pageList);
                }
                pageList.push(config.id);
            }
            else if (config.type === E_BattleReportType.TrialTower) {
                this.trialTowerStage.put(config.id, 1);
            }
            else if (config.type === E_BattleReportType.WorldBoss) {
                this.worldBossStage.put(config.id, 1);
            }
            else if (config.type === E_BattleReportType.WorldBoss2) {
                this.worldBossStage2.put(config.id, 1);
            }
            else if (config.type === E_BattleReportType.WorldBoss3) {
                this.worldBossStage3.put(config.id, 1);
            }
            else if (config.type === E_BattleReportType.LegionBoss) {
                this.legionBossStage.put(config.id, 1);
            }
        }
    }


    /**
     * 获取下一关配置
     * @param id 
     * @returns 
     */
    public static getNextStage(id: number | CDStage): CDStage {
        const stageID = (id instanceof CDStage) ? id.id : id;
        const nextID = this.nextStage.get(stageID) || 0;
        if (0 === nextID) {
            return null;
        }
        const config = CDStage.getData(nextID);
        return config;
    }
}